//
//  HeroController.m
//  ProjectA
//
//  Created by lanouhn on 15/5/22.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "HeroController.h"
#import "LoadingTableViewCell.h"
#import "SecondCell.h"
#import "NetWorkEngine.h"
#import "SecondModel.h"
#import "SecondController.h"
#define kURL @"http://lol.video.luckyamy.com/api/?cat=hero&page=%d&ap=lolg&ver=1.1"


@interface HeroController ()<NetworkEngineDelegate>
@property (nonatomic ,retain)UILabel *lable;
@property (nonatomic ,retain)NSMutableArray *dataSouce;
@property (nonatomic ,assign)int pageNumber;
- (void)_startLoadingDataWithPage:(int)page;
@end

@implementation HeroController
- (void)dealloc {

    [_lable release];
    [_dataSouce release];
    [super dealloc];
}
- (UILabel *)lable {
   
    if (!_lable) {
        self.lable = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
        _lable.text = @"英雄";
        _lable.textAlignment = NSTextAlignmentCenter;
        _lable.textColor = [UIColor whiteColor];
    }
    return _lable ;
}
- (NSArray *)dataSouce{
 
    if (!_dataSouce) {
        self.dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"英雄";
    self.navigationItem.titleView = self.lable;
    [self.tableView registerClass:[LoadingTableViewCell class] forCellReuseIdentifier:@"LOAD"];
    [self.tableView registerClass:[SecondCell class] forCellReuseIdentifier:@"CELL"];
    
    self.refreshControl = [[[UIRefreshControl alloc]init]autorelease];
    [self.refreshControl addTarget:self action:@selector(handleRefershaction:) forControlEvents:(UIControlEventValueChanged)];
    self.pageNumber = 1;
    [self _startLoadingDataWithPage:self.pageNumber];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tabBarItem.title = @"返回";
}
- (void)_startLoadingDataWithPage:(int)page {
 
    NetworkEngine *engine = [NetworkEngine engineWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURL,self.pageNumber]] parameters:nil delegate:self];
    [engine start];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
 
    // Dispose of any resources that can be recreated.
}
- (void)handleRefershaction:(UIRefreshControl *)sender{
   
    self.pageNumber = 1;
    [self _startLoadingDataWithPage:self.pageNumber];
}
- (void)networkDidFinishLoading:(NetworkEngine *)engine withInfo:(id)info{
  
   
    [self.refreshControl endRefreshing];
    if (self.pageNumber == 1) {
        [self.dataSouce removeAllObjects];
    }
    self.pageNumber ++;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:info options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",array);
    for (NSDictionary *dic in array) {
        SecondModel *model = [SecondModel secondModelWithDic:dic];
   
        [self.dataSouce addObject:model];
    }
    [self.tableView reloadData];
    
}
#pragma tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.dataSouce.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.dataSouce.count) {
        return [tableView dequeueReusableCellWithIdentifier:@"LOAD" forIndexPath:indexPath];
    }
    SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    SecondModel *model = self.dataSouce[indexPath.row];
    NSLog(@"%@",model);
    [cell cellWithModel:model];
  
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.dataSouce.count) {
       return  30;
    }
    return 70;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell isKindOfClass:[LoadingTableViewCell class]] && self.dataSouce.count == indexPath.row) {
        [(LoadingTableViewCell *)cell startLoading];
        [self _startLoadingDataWithPage:self.pageNumber];
        
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondController *secondVC = [[SecondController alloc]init];
    SecondModel *model = self.dataSouce[indexPath.row];
    secondVC.ID = model.ID;
    secondVC.tittle = model.title;
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
